﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;

namespace LDSP.UI.Admin
{
    public partial class MngCatalog : LDSP.BRL.BaseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                this.InitDataDynamicBefore();
                if (!IsPostBack)
                {
                    this.InitData();
                    this.BindTree();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected TreeNode m_selectedNode = null;
        protected Catalog m_catalog = null;
        protected Catalog m_parentCatalog = null;

        protected int ParentID
        {
            get { return this.GetVSInt("ParentID"); }
            set
            {
                this.SetVS("ParentID", value);
                m_parentCatalog = null;
            }
        }
        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_catalog = null;
            }
        }
        protected Catalog Catalog
        {
            get
            {
                if (m_catalog == null)
                {
                    m_catalog = new Catalog();
                    if (LastID != 0)
                    {
                        m_catalog.GetProperties(LastID);
                    }
                }
                return m_catalog;
            }
        }
        protected Catalog ParentCatalog
        {
            get
            {
                if (m_parentCatalog == null)
                {
                    m_parentCatalog = new Catalog();
                    if (ParentID != 0)
                    {
                        m_parentCatalog.GetProperties(ParentID);
                    }
                }
                return m_parentCatalog;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (Catalog.ID == 0)
            {
                LastID = 0;
            }
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitData()
        {
            this.LastID = 0;
            this.InitDataSelect();
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = Catalog.Name;
            this.txt_comment.Text = Catalog.Comment;
            this.lbl_parentCatalog.Text = IsAdd ? ParentCatalog.Name : Catalog.Parent.Name;
            this.SetStatus();
        }
        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (Catalog.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            Catalog.Name = this.txt_name.Text.Trim();
            Catalog.Comment = this.txt_comment.Text;
            if (IsAdd)
            {
                Catalog.ParentID = ParentID;
            }
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            Catalog.Add();
            this.InsertNode();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            this.SetData();
            Catalog.Update();
            this.UpdateNode();
            this.JSAlert("更新成功！");
        }
        protected void InitDataDynamicBefore()
        {
            if (tre_obj.SelectedNode != null)
            {
                m_selectedNode = tre_obj.SelectedNode;
                tre_obj.SelectedNode.Selected = false;
            }
        }

        protected void BindTree()
        {
            this.tre_obj.Nodes.Clear();
            List<Catalog> rootCatalogs = new Catalog().GetRootListByDefault();
            for (int i = 0; i < rootCatalogs.Count; i++)
            {
                TreeNode nd = new TreeNode();
                nd.Text = rootCatalogs[i].Name;
                nd.Value = rootCatalogs[i].ID.ToString();
                this.tre_obj.Nodes.Add(nd);
                if (rootCatalogs[i].ID == ParentID)
                {
                    nd.Select();
                    Expand(nd);
                }
                this.BindTreeAssis(nd, rootCatalogs[i].ID);
            }
        }
        protected void BindTreeAssis(TreeNode parent, int parentID)
        {
            List<Catalog> catalogs = new Catalog().GetListByParentID(parentID);
            for (int i = 0; i < catalogs.Count; i++)
            {
                TreeNode nd = new TreeNode();
                nd.Text = catalogs[i].Name;
                nd.Value = catalogs[i].ID.ToString();
                parent.ChildNodes.Add(nd);
                if (catalogs[i].ID == ParentID)
                {
                    nd.Select();
                    Expand(nd);
                }
                this.BindTreeAssis(nd, catalogs[i].ID);
            }
        }
        protected void Expand(TreeNode node)
        {
            if (node.Parent != null)
            {
                node.Parent.Expand();
                Expand(node.Parent);
            }
        }
        protected void SelectNode()
        {
            if (m_selectedNode != null)
            {
                m_selectedNode.Selected = true;
            }
        }
        protected void InsertNode()
        {
            TreeNode nd = new TreeNode();
            nd.Text = Catalog.Name;
            nd.Value = Catalog.ID.ToString();
            if (ParentID == 0)
            {
                this.tre_obj.Nodes.Add(nd);
            }
            else
            {
                if (m_selectedNode != null)
                {
                    m_selectedNode.ChildNodes.Add(nd);
                }
                else
                {
                    this.BindTree();
                }
            }
        }
        protected void UpdateNode()
        {
            if (m_selectedNode != null)
            {
                m_selectedNode.Text = Catalog.Name;
            }
            else
            {
                this.BindTree();
            }
        }
        protected void DeleteNode(TreeNode nd)
        {
            if (nd != null)
            {
                if (nd.Parent == null)
                {
                    tre_obj.Nodes.Remove(nd);
                }
                else
                {
                    nd.Parent.ChildNodes.Remove(nd);
                }
            }
        }

        protected void GetCheckedNodes(List<TreeNode> checkedNodes, TreeNodeCollection children)
        {
            for (int i = 0; i < children.Count; i++)
            {
                if (children[i].Checked)
                {
                    checkedNodes.Add(children[i]);
                }
                else
                {
                    GetCheckedNodes(checkedNodes, children[i].ChildNodes);
                }
            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.InitData();
                this.SelectNode();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_addRoot_Click(object sender, EventArgs e)
        {
            try
            {
                this.ParentID = 0;
                this.InitData();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();
                this.SelectNode();
                //this.BindTree();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                List<TreeNode> checkedNodes = new List<TreeNode>();
                this.GetCheckedNodes(checkedNodes, this.tre_obj.Nodes);
                for (int i = 0; i < checkedNodes.Count; i++)
                {
                    this.DeleteNode(checkedNodes[i]);
                    Catalog ett = new BRL.Catalog();
                    ett.GetProperties(this.GetInt(checkedNodes[i].Value));
                    ett.Delete();//todo 级联删除子类别，法规，标准，企业可见性关系
                }
                this.SelectNode();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void tre_obj_SelectedNodeChanged(object sender, EventArgs e)
        {
            ParentID = this.GetInt(this.tre_obj.SelectedValue);
            LastID = ParentID;
            this.InitDataSelect();
        }
    }
}